package com.bkhc.common.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Date;

import com.bkhc.common.enu.ProductStatusEnum;
import org.hibernate.transform.AliasToBeanResultTransformer;

import javax.persistence.Column;

public class TransObject extends AliasToBeanResultTransformer {
    private Class resultClass;

    public TransObject(Class resultClass) {
        super(resultClass);
        this.resultClass = resultClass;
    }

    private static final long serialVersionUID = 1L;

    public Object transformTuple(Object[] tuple, String[] aliases) {
        Object obj = null;
        try {
            obj = resultClass.newInstance();
        } catch (InstantiationException e1) {
            e1.printStackTrace();
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        }
        Method[] methods = resultClass.getMethods();// 返回这个类里面方法的集合
        Field[] fields = resultClass.getDeclaredFields();
        for (int k = 0; k < aliases.length; k++) {
            String aliase = getKey(fields, aliases[k]);
            char[] ch = aliase.toCharArray();
            ch[0] = Character.toUpperCase(ch[0]);
            String s = new String(ch);
            String[] names = new String[] { ("set" + s).intern(), ("get" + s).intern(), ("is" + s).intern(),
                    ("read" + s).intern() };
            Method setter = null;
            Method getter = null;
            int length = methods.length;
            for (int i = 0; i < length; ++i) {
                Method method = methods[i];
                /**
                 * 检查该方法是否为公共方法,如果非公共方法就继续
                 */
//                if (!Modifier.isPublic(method.getModifiers()))
//                    continue;
                String methodName = method.getName();

                for (String name : names) {
                    if (name.equals(methodName)) {
                        if (name.startsWith("set") || name.startsWith("read"))
                            setter = method;
                        else if (name.startsWith("get") || name.startsWith("is"))
                            getter = method;

                    }
                }
            }
            if (getter != null) {
                Object[] param = buildParam(getter.getReturnType().getName(), tuple[k]);
                try {
                    setter.invoke(obj, param);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return obj;
    }

    private final static Object[] buildParam(String paramType, Object value) {
        Object[] param = new Object[1];
        if (paramType.equalsIgnoreCase("java.lang.String")) {
            param[0] = (String) (value);
        } else if (paramType.equalsIgnoreCase("int") || paramType.equalsIgnoreCase("java.lang.Integer")) {
            if (value instanceof BigDecimal) {
                param[0] = ((BigDecimal) (value)).intValue();
            } else {
                param[0] = (Integer) (value);
            }
        } else if (paramType.equalsIgnoreCase("long") || paramType.equalsIgnoreCase("java.lang.Long")) {
            param[0] = (Long) (value);
        } else if (paramType.equalsIgnoreCase("double") || paramType.equalsIgnoreCase("java.lang.Double")) {
            param[0] = (Double) (value);
        } else if (paramType.equalsIgnoreCase("BigDecimal") || paramType.equalsIgnoreCase("java.math.BigDecimal")) {
            param[0] = (BigDecimal) (value);
        } else if (paramType.equalsIgnoreCase("float") || paramType.equalsIgnoreCase("java.lang.Float")) {
            param[0] = (Float) (value);
        } else if (paramType.equalsIgnoreCase("char") || paramType.equalsIgnoreCase("Character")) {
            param[0] = (char) (value);
        } else if (paramType.equalsIgnoreCase("date") || paramType.equalsIgnoreCase("java.util.Date")) {
            param[0] = (Date) (value);
        }else if (paramType.equalsIgnoreCase("com.bkhc.common.enu.ProductStatusEnum")) {
            param[0] = ProductStatusEnum.getProductStatusEnum((int)(value));
        }
        else if (paramType.equalsIgnoreCase("com.bkhc.common.enu.ProductStatusEnum")) {
            param[0] = ProductStatusEnum.getProductStatusEnum((int)(value));
        }
        return param;
    }

    private String getKey(Field[] fields, String aliase) {
        String result = aliase;
        try {
            for (Field f : fields) {
                Column anno = f.getAnnotation(Column.class);
                if (anno != null) {
                    if (anno.name().equals(aliase)) {
                        result = f.getName();
                        break;
                    }
                }
                continue;
            }
        } catch (Exception e) {
        }
        return result;
    }
}
